package com.catm.haitao.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import proguard.annotation.NotProguard;

/**
 * 通用标题
 *
 * @author Taoweisong
 */
@NotProguard
public class TitleLayout extends RelativeLayout {

    private View mTitleLayoutView;
    private RelativeLayout mRelativeLayout_Title_Left;
    private RelativeLayout mRelativeLayout_Title_Right;
    private TextView mTextTitle;
    private ImageView mImageLeft;
    private ImageView mImageRight;
    private TextView mLeftTextView;
    private Context mContext;

    public TitleLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TitleLayout(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(getResources().getIdentifier("moxie_client_common_title", "layout", context.getPackageName()), this);
        mTextTitle = (TextView) view.findViewById(getResources().getIdentifier("TextView_Title", "id", context.getPackageName()));
        mImageLeft = (ImageView) view.findViewById(getResources().getIdentifier("Moxie_Client_ImageView_Left", "id", context.getPackageName()));
        mImageRight = (ImageView) view.findViewById(getResources().getIdentifier("Moxie_Client_ImageView_Right", "id", context.getPackageName()));
        mTitleLayoutView = view.findViewById(getResources().getIdentifier("Moxie_Client_RelativeLayout_Title", "id", context.getPackageName()));
        mRelativeLayout_Title_Left = (RelativeLayout) view.findViewById(getResources().getIdentifier("Moxie_Client_RelativeLayout_Title_Left", "id", context.getPackageName()));
        mRelativeLayout_Title_Right = (RelativeLayout) view.findViewById(getResources().getIdentifier("Moxie_Client_RelativeLayout_Title_Right", "id", context.getPackageName()));
        mLeftTextView = (TextView) view.findViewById(getResources().getIdentifier("moxie_client_actionbar_left_text", "id", context.getPackageName()));
    }

    public void setTitle(int title) {
        try {
            mTextTitle.setText(title);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void setTitle(String title) {
        mTextTitle.setText(title);
    }

    public View getLeftTextView(){
        return mLeftTextView;
    }

    public ImageView getLeftImage() {
        return mImageLeft;
    }

    public RelativeLayout getRelativeLayout_Title_Left() {
        return  mRelativeLayout_Title_Left;
    }
    public RelativeLayout getRelativeLayout_Title_Right() {
        return  mRelativeLayout_Title_Right;
    }

    public ImageView getRightImage() {
        return mImageRight;
    }

    public void setLeftTextVisibility(int visibility){
        mLeftTextView.setVisibility(visibility);
    }

    public void initTitleLayout(){

        try {
            //向下兼容。。
            String textColor = bannerTxtColor;

            int textColorInt = Color.parseColor(textColor);
            if(textColor.toLowerCase().equalsIgnoreCase("#ffffff") || textColor.toLowerCase().equalsIgnoreCase("#ffffffff") ||
                    textColor.toLowerCase().equalsIgnoreCase("#000000")|| textColor.toLowerCase().equalsIgnoreCase("#ff000000")){
                if(textColor.toLowerCase().equalsIgnoreCase("#ffffff") ||
                        textColor.toLowerCase().equalsIgnoreCase("#ffffffff")){
                    mImageLeft.setBackgroundResource(mContext.getResources().getIdentifier("moxie_client_banner_back", "drawable", mContext.getPackageName()));
                    mImageRight.setBackgroundResource(mContext.getResources().getIdentifier("moxie_client_banner_refresh", "drawable", mContext.getPackageName()));
                } else {
                    mImageLeft.setBackgroundResource(mContext.getResources().getIdentifier("moxie_client_banner_back_black", "drawable", mContext.getPackageName()));
                    mImageRight.setBackgroundResource(mContext.getResources().getIdentifier("moxie_client_banner_refresh_black", "drawable", mContext.getPackageName()));
                }
                mImageLeft.setVisibility(VISIBLE);
                mImageRight.setVisibility(VISIBLE);

            } else {
                mImageLeft.setVisibility(GONE);
                mImageRight.setVisibility(GONE);
            }
            mTitleLayoutView.setBackgroundColor(Color.parseColor(bannerBgColor));
            mTextTitle.setTextColor(textColorInt);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private String bannerBgColor = "#000000";
    private String bannerTxtColor = "#ffffff";
}
